﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RollBackLib.src.ui.Dialogs
{
    public partial class PreferencesDialog : Form
    {
        private Dictionary<string, object> map = new Dictionary<string, object>();
        public PreferencesDialog()
        {
            InitializeComponent();
            this.treeView1.AfterSelect += new TreeViewEventHandler(treeView1_AfterSelect);
        }

        void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            string fullPath = e.Node.FullPath;
            if (map.ContainsKey(fullPath))
            {
                Object o = map[fullPath];
                if (o != null)
                {
                    Control ctl = (Control)o;
                    this.tabControl1.Controls.Clear();
                    TabPage page = new TabPage();
                    page.Controls.Add(ctl);
                    page.Text = fullPath;
                    this.tabControl1.Controls.Add(page);
                }
            }
        }

        public void addPreference(string root, string name, Control control)
        {
            string key = root + "\\" + name;
            // first find the path to the parent node
            TreeNode[] nodes = this.treeView1.Nodes.Find(root,false);
            TreeNode rootNode = null;
            if (nodes == null || nodes.Length == 0)
            {
                // parent node does not exist hence create it.
                rootNode = new TreeNode(root);
                this.treeView1.Nodes.Add(rootNode);
            }
            else
            {
                rootNode = nodes[0];
            }

            TreeNode tn = null;
            if (name == null)
            {
                tn = rootNode;
            }
            else
            {
                // add the name to the parent root node.
                tn = rootNode.Nodes.Add(name);
            }

            map[tn.FullPath] = control;
        }

    }
}
